﻿///////////////////////////////////////////////////////////////////////////////
//
//  This source is part of the  "Managed Task Framework.NET Runtime"  (MTF.NET Runtime).
//
//
//  Copyright (c) 2011, Multicore Consulting Limited (UK)
//  All rights reserved.
//  http://www.multicoreconsulting.co.uk
//
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions
//  are met:
//
//  -  Redistributions of source code must retain the above copyright
//     notice, this list of conditions and the following disclaimer.
//  -  Redistributions in binary form must reproduce the above copyright
//     notice, this list of conditions and the following disclaimer in
//     the documentation and/or other materials provided with the
//     distribution.
//  -  Neither the name of Multicore Consulting Limited nor the names of
//     its contributors may be used to endorse or promote products
//     derived from this software without specific prior written
//     permission.
//
//  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
//  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
//  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
//  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
//  COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
//  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
//  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
//  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
//  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
//  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
//  POSSIBILITY OF SUCH DAMAGE.
//
///////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MTFOrg.MTF.MTFRuntime;
using MTFOrg.MTF.MTFCore.Messages;

namespace MTFOrg.MTF.MTFCore
{
    /// <summary>
    /// Class implementing the relay object.
    /// </summary>
    partial class Relay
    {
        /// <summary>
        /// Called to initialize this task.
        /// </summary>
        public override void OnInitialize()
        {
            base.OnInitialize();
        }

        /// <summary>
        /// Called when an event occurs on a connection to this object.
        /// </summary>
        /// <param name="cfeArgs">Connection-fired event arguments</param>
        protected sealed override void OnMessage(ConnectionFiredEventArgs cfeArgs)
        {
            IConnection connection = cfeArgs.Connection;

            // Route message onwards via opposite connection.
            IMessage message = cfeArgs.Message;
            if (message is EventMsg)
            {
                EventMsg eventMsg = message as EventMsg;
                IConnection oppositeConnection = ProducerConnectionPoint.Connections[0];
                int elemNum, linkNum;
                ConnectionHelper.MapProducerToConsumer(oppositeConnection, eventMsg.ElementNumber, eventMsg.LinkNumber, out elemNum, out linkNum);
                TaskManager.SendMessage(new EventMsg(elemNum, linkNum, eventMsg.Event, eventMsg.ProducerKey), oppositeConnection);
            }
            else if (message is QueryMsg)
            {
                QueryMsg queryMsg = message as QueryMsg;
                IConnection oppositeConnection = ConsumerConnectionPoint.Connections[0];
                int elemNum, linkNum;
                ConnectionHelper.MapConsumerToProducer(oppositeConnection, queryMsg.ElementNumber, queryMsg.ProducerLinkNumber, out elemNum, out linkNum);
                TaskManager.SendMessage(new QueryMsg(elemNum, linkNum), oppositeConnection);
            }
            else if (message is ReleaseMsg)
            {
                ReleaseMsg releaseMsg = message as ReleaseMsg;
                IConnection oppositeConnection = ConsumerConnectionPoint.Connections[0];
                int elemNum, linkNum;
                ConnectionHelper.MapConsumerToProducer(oppositeConnection, releaseMsg.ElementNumber, releaseMsg.ProducerLinkNumber, out elemNum, out linkNum);
                TaskManager.SendMessage(new ReleaseMsg(releaseMsg.EventToRelease, releaseMsg.ReleaseType, elemNum, linkNum, releaseMsg.ProducerKey, releaseMsg.NeedResponse), oppositeConnection);
            }
            else if (message is ReleasedMsg)
            {
                ReleasedMsg releasedMsg = message as ReleasedMsg;
                IConnection oppositeConnection = ProducerConnectionPoint.Connections[0];
                int elemNum, linkNum;
                ConnectionHelper.MapProducerToConsumer(oppositeConnection, releasedMsg.ElementNumber, releasedMsg.DstLink, out elemNum, out linkNum);
                TaskManager.SendMessage(new ReleasedMsg(elemNum, linkNum, releasedMsg.DstSlot), oppositeConnection);
            }

            base.OnMessage(cfeArgs);
        }
    }
}